﻿using System.Security.Cryptography;
using System.Text;

namespace ERP.MD5
{
    public class MD5
    {
        // 生成盐
        public static string GenerateSalt(int size = 16)
        {
            var saltBytes = new byte[size];
            RandomNumberGenerator.Fill(saltBytes);
            return Convert.ToBase64String(saltBytes);
        }

        // 密码加盐哈希
        public static string HashPassword(string password, string salt)
        {
            var sha256 = SHA256.Create();
            var combined = Encoding.UTF8.GetBytes(password + salt);
            var hash = sha256.ComputeHash(combined);
            return Convert.ToBase64String(hash);
        }

        // 验证密码
        public static bool VerifyPassword(string inputPassword, string salt, string storedHash)
        {
            var hashOfInput = HashPassword(inputPassword, salt);
            return hashOfInput == storedHash;
        }
    }
}
